import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { Message } from 'element-ui'
import router, { resetRouter } from '@/router'
import $httpJSON from '@/utils/requestJSON';
import store from '@/store'
import qs from 'qs';

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    roles: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password, type } = userInfo
    return new Promise((resolve, reject) => {
      $httpJSON.post('/login/' + type, {
        account: username,
        password: password
      }).then(response => {
        if (response.success) {
          const data = response.data
          commit('SET_TOKEN', data.token)
          setToken(data.token)
          let role = ['admin']
          localStorage.setItem('role', role[0])
          commit('SET_ROLES', role)
          commit('SET_NAME', data.account)
          store.dispatch('permission/generateRoutes', role).then(res => {
            router.addRoutes(res)
          })
          resolve(response)
        } else  {
          Message({
            message: response.msg,
            type: 'error',
            duration: 2000
          })
        }
        
      }).catch(error => {
        
        // commit('SET_TOKEN', 'data.token')
        // setToken('data.token')
        // let role = ['admin']
        // localStorage.setItem('role', role[0])
        // commit('SET_ROLES', role)
        // commit('SET_NAME',' data.account')

        // console.log(666)
        // store.dispatch('permission/generateRoutes', role).then(res => {
        //   router.addRoutes(res)
        // })
        resolve()
        // reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    console.log('getInfo')
       const roles = [localStorage.getItem('role')]
      commit('SET_ROLES', roles)
      return roles
    // return new Promise((resolve, reject) => {
    //   const roles = [localStorage.getItem('role')]
    //   commit('SET_ROLES', roles)
    //   return roles
    // })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      removeToken() // must remove  token  first
      resetRouter()
      commit('RESET_STATE')
      resolve()
      // logout(state.token).then(() => {
      //   removeToken() // must remove  token  first
      //   resetRouter()
      //   commit('RESET_STATE')
      //   resolve()
      // }).catch(error => {
      //   reject(error)
      // })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

